import { hashHistory } from 'react-router';

//接口返回成功标志
export const SUCCESS = 200;
export const TOKEN = "tokenInfo";

export const TIPS_LOADING_DURATION = 3;
export const TIPS_INFO_DURATION = 1.5;

//服务器配置
export const DEFAULT_TITLE = "Mall商城";
export const PRODUCT_SERVER_URL = "http://119.45.229.46/appApi";
export const DEV_SERVER_URL = "http://192.168.1.33:8002/appApi";

//微信配置
export const APPID = "wx969e42c277d5f824";
export const REDIRECT_URI = "http://www.51vim.com/views/index.html";
export const WEIXIN_LOGIN_URL = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID+"&redirect_uri="+REDIRECT_URI+"&response_type=code&scope=SCOPE&state=STATE#wechat_redirect"

//微信登录
export function weixinLogin(){
	window.location.href = common.WEIXIN_LOGIN_URL;
}

//返回
export function goBack(){
	window.history.back();
}

//首页
export function gotoHome(){
	hashHistory.replace("/");
}

//登录页面
export function gotoLogin(){
	hashHistory.push("/login");
}

//注册页面
export function gotoRegister(){
	hashHistory.push("/register");
}

//我的页面
export function gotoUserInfo(){
	hashHistory.push("/userInfo");
}

//购物车页面
export function gotoUserCart(){
	hashHistory.push("/userCart");
}

//商品详情页面
export function gotoGoodsDetail(goodsId){
	hashHistory.push("/goodsDetail/"+goodsId);
}

//订单预览页面
export function gotoOrderPreview({cartIds}){
	hashHistory.push("/orderPreview/"+cartIds);
}

//支付页面
export function gotoOrderPay({orderId}){
	hashHistory.push("/orderPay/"+orderId);
}

//用户订单列表页面
export function gotoUserOrderList(orderStatus){
	hashHistory.push("/userOrderList/"+orderStatus);
}

//用户账户页面
export function gotoUserCash(){
	hashHistory.push("/userCash");
}

//商品搜索结果页面
export function gotoGoodsSearch({searchType, searchKeyword}){
	hashHistory.push("/goodsSearch/"+searchType+"/"+searchKeyword);
}

//保存token
export function saveToken(token){
	storeSet(TOKEN, token);
}

//删除token
export function removeToken(){
	storeRemove(TOKEN);
}

//当前登录用户ID
export function userId(){
	if(storeGet("tokenInfo")){
		return storeGet("tokenInfo").memberInfo.id;
	}else{
		return "";
	}
}

//当前是否处于开发环境
export function isDevEnv(){
	let env = process.env;
	if(env.NODE_ENV == 'dev'){
		return true;
	}else{
		return false;
	}
}

//开发、线上环境转换url
export function urlTransmit(url){
	let env = process.env;
	if(env.NODE_ENV == 'dev'){
		return DEV_SERVER_URL + url;
	}else{
		return PRODUCT_SERVER_URL + url;
	}
}

export const fenToYuan = (fen) =>{
    var num = fen;
    num=fen*0.01;
    num+='';
    var reg = num.indexOf('.') >-1 ? /(\d{1,3})(?=(?:\d{3})+\.)/g : /(\d{1,3})(?=(?:\d{3})+$)/g;
    num=num.replace(reg,'$1');
    num = toDecimal2(num)
    return num
};

export const toDecimal2 = (x) => {
	var f = parseFloat(x);
	if (isNaN(f)) {
	  return false;
	}
	var f = Math.round(x * 100) / 100;
	var s = f.toString();
	var rs = s.indexOf('.');
	if (rs < 0) {
	  rs = s.length;
	  s += '.';
	}
	while (s.length <= rs + 2) {
	  s += '0';
	}
	return s;
}

export function cacheExists(name){
	let cacheData = sessionStorage.getItem(name);
	if(cacheData){
		return true;
	}else{
		return false;
	}
}
export function cacheClear(name){
	return sessionStorage.clear();
}
export function cacheRemove(name){
	return sessionStorage.removeItem(name);
}
export function cacheGet(name){
	return JSON.parse(sessionStorage.getItem(name));
}
export function cacheSet(name, data){
	sessionStorage.setItem(name, JSON.stringify(data));
}

export function storeRemove(name){
	return localStorage.removeItem(name);
}
export function storeGet(name){
	return JSON.parse(localStorage.getItem(name));
}
export function storeSet(name, data){
	localStorage.setItem(name, JSON.stringify(data));
}

//数组位置
export function indexInArray(arr,value){
    for(var i = 0; i < arr.length; i++){
        if(value === arr[i]){
            return i;
        }
    }
    return -1;
}

//是否在数组中
export function isInArray(arr,value){
    for(var i = 0; i < arr.length; i++){
        if(value === arr[i]){
            return true;
        }
    }
    return false;
}
	